package com.mango.request;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class Algorithm {
    /**
     * MD5加密
     *
     * @param string 原文
     * @return 密文
     */
    public static String MD5Encrypt(String string) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(string.getBytes());
            return toHexString(md.digest());
        } catch (Exception exception) {
            return "";
        }
    }

    /**
     * SHA-256加密
     *
     * @param string 原文
     * @return 密文
     */
    public static String SHA256Encrypt(String string) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(string.getBytes());
            return toHexString(md.digest());
        } catch (Exception exception) {
            return "";
        }
    }

    /**
     * 3DES加密
     *
     * @param string 原文
     * @param key    密钥
     * @return 密文
     */
    public static String DesEncrypt(String string, String key) {
        try {
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("GBK"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes("GBK"));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            return toHexString(cipher.doFinal(string.getBytes("GBK")));
        } catch (Exception exception) {
            return "";
        }
    }

    /**
     * 3DES解密
     *
     * @param string 密文
     * @param key    密钥
     * @return 原文
     */
    public static String DesDecrypt(String string, String key) {
        try {
            byte[] bytes = convertHexString(string);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("GBK"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes("GBK"));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            return new String(cipher.doFinal(bytes));
        } catch (Exception exception) {
            return "";
        }
    }

    /**
     * String转Byte数组
     *
     * @param string 字符串
     * @return Byte数组
     */
    private static byte[] convertHexString(String string) {
        byte[] bytes = new byte[string.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            String hexString = string.substring(i * 2, i * 2 + 2);
            int byteValue = Integer.parseInt(hexString, 16);
            bytes[i] = (byte) byteValue;
        }
        return bytes;
    }

    /**
     * Byte数组转String
     *
     * @param bytes Byte数组
     * @return 字符串
     */
    private static String toHexString(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String plainText = Integer.toHexString(0xFF & b);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }
        return hexString.toString().toLowerCase();
    }
}
